package com.ambow.gateway.controller;

import com.ambow.common.redis.service.RedisServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author 周志伟
 * @projectname 项目名称: ${project_name}
 * @classname: DrawImageController
 * @description:
 * @date 2018/6/8：14:20
 */
@Controller
@RequestMapping("/appImageCode")
@Api(tags = {"获取验证码"})
public class DrawImageController  {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisServiceImpl redisUtils;

    @RequestMapping("/img/validate")
    @ResponseBody
    @ApiOperation(value="获取验证码",notes="获取验证码",httpMethod = "POST")
    public void pwdApply( HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        IdentifyingCode randomValidateCode = new IdentifyingCode();

        try {
            String val =  randomValidateCode.getRandcode(request, response);// 输出图片方法
            String key="imgcode_"+val;
            logger.info("验证码KEY==="+key);
            redisUtils.set(key,val,5l, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        }
    }
}
